package page

import (
	"math"
)

type Paging struct {
	PreviousPage int   //上一页
	NextPage     int   //下一页
	PageNum      int   //当前页
	Pagesize     int   //每页条数
	Total        int   //总条数
	PageCount    int   //总页数
	Pages        []int //页数集合
	StartNum     int   //起始行
}

//创建分页
func CreatePaging(pageNum, pagesize, total int) *Paging {
	if pageNum < 1 {
		pageNum = 1
	}
	if pagesize < 1 {
		pagesize = 10
	}
	

	page_count := math.Ceil(float64(total) / float64(pagesize))
	strat_num := pagesize * (pageNum - 1)
	paging := new(Paging)
	paging.PageNum = pageNum
	paging.Pagesize = pagesize
	paging.Total = total
	paging.PageCount = int(page_count)
	paging.StartNum = strat_num
	for i := 1; i <= paging.PageCount; i++ {
		paging.Pages = append(paging.Pages, i)
	}
	if paging.PageNum-1 <= 0 {
		paging.PreviousPage = 1
	} else {
		paging.PreviousPage = paging.PageNum - 1
	}
	if paging.PageNum+1 >= paging.PageCount {
		paging.NextPage = paging.PageCount
	} else {
		paging.NextPage = paging.PageNum + 1
	}

	return paging
}
